package com.wangjiang.k8s.jvm.test.holder;

import com.wangjiang.k8s.jvm.test.domain.user.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 用户持有类缓存
 * @author wangjiang
 * @date 2020-10-10 10:06:05
 */
@Slf4j
public class UserHolder {

    private Map<String, UserEntity> userEntityMap = new ConcurrentHashMap<>();

    public void put(UserEntity userEntity) {
        log.info("put a user == {}", userEntity.getName());
        userEntityMap.putIfAbsent(userEntity.getName(), userEntity);
        log.info("now holder size: {}", userEntityMap.size());
    }

    public void delete(String username) {
        if (userEntityMap.containsKey(username)) {
            userEntityMap.remove(username);
        }
    }

    public UserEntity get(String username) {
        return userEntityMap.getOrDefault(username, null);
    }
}
